package com.lu.adog.base;

import android.app.Activity;
import android.app.Application;

import java.util.ArrayList;

/**
 * @author Yue
 * @date 2017/11/13 18:16
 */

public class BaseApp extends Application {
    private ArrayList<Activity> activityList;

    private static BaseApp baseApp;

    protected static BaseApp getBaseApp() {
        return baseApp;
    }

    @Override
    public void onCreate() {
        baseApp = this;
        if (activityList == null) {
            activityList = new ArrayList<>();
        }
        super.onCreate();
    }

    public void addActivity(Activity activity) {
        if (activityList == null) {
            activityList = new ArrayList<>();
        }
        if (activityList != null && !activityList.contains(activity)) {
            activityList.add(activity);
        }
    }

    public void finishActivityExcept(Activity activity) {
        if (activityList == null) {
            return;
        }
        for (Activity act : activityList) {
            if (activity != act) {
                act.finish();
            }
        }
        activityList.clear();
        activityList.add(activity);
    }

    public void finishAllActivity() {
        if (activityList == null) return;
        for (Activity act : activityList) {
            act.finish();
        }
        activityList.clear();
    }

    public void removeActivity(Activity activity) {
        activityList.remove(activity);
    }

    public void exit() {
        finishAllActivity();
        System.exit(0);
    }
}
